<template>
  <div>
    <h4 class="font-weight-bold">
      <span class="text-muted font-weight-light">风控接口 /</span> 风控对象执行历史
    </h4>

    <b-card class="mb-4">
        <div>
            <Table :data="table.data" :columns="table.columns" :loading="table.loading" stripe border class="mt-2"></Table>
            <Page show-elevator show-total size="small" class="mt-2" :total="table.total" :current="table.currentPage"
                    @on-change="(pageNum) => {
                        let self = this;
                        table.pageChange.call(self,pageNum);
                    }"></Page>
        </div>
    </b-card>
    <!-- <sky-table :data="table.data" :columns="table.columns" :show-index="table.showIndex" :show-pagination="true" class="mt-0"></sky-table> -->

  </div>
</template>

<script>
function padLeftZero (str) {
    return ('00' + str).substr(str.length);
};
function formatDate (longStr, fmt) {
   let date = new Date(longStr);

    if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
    }
    let o = {
        'M+': date.getMonth() + 1,
        'd+': date.getDate(),
        'h+': date.getHours(),
        'm+': date.getMinutes(),
        's+': date.getSeconds()
    };
    for (let k in o) {
        if (new RegExp(`(${k})`).test(fmt)) {
            let str = o[k] + '';
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str));
        }
    }
    return fmt;
};
export default {
    name: "riskApiRunLog",
    metaInfo: {
        title: "风控接口 - 风控对象执行历史"
    },
    components: {
    },
    data() {
        return {
            form: {
                name: '',
                applyObject: '',
                switch: false
            },
            table: {
                showIndex: false,
                data: [],
                columns: [
                    {
                        title: "调用方",
                        key: "callerName"
                    },
                    {
                        title: "风控服务",
                        key: "serviceName"
                    },
                    {
                        title: "服务节点",
                        key: "serviceNodeName"
                    },
                    {
                        title: "风控对象类型",
                        key: "objectType",
                        render: (h,context) => {
                            let objectTypeName = '';
                            if(context.row.objectType == 'customer'){
                                objectTypeName = '企业客户'
                            }
                            return (
                                <div>
                                    {objectTypeName}
                                </div>
                            );
                        }
                    },
                    {
                        title: "风控对象",
                        key: "objectName"
                    },
                    {
                        title: "执行时间",
                        key: "createDate",
                        render: (h, params) => {
                            let createDate = formatDate(params.row.createDate,'yyyy-MM-dd hh:mm:ss');
                            return (
                                <div>
                                    {createDate}
                                </div>
                            );
                        }
                    }
                ],
                // 是否载入中
                loading: false,
                // 总页数
                total: 0,
                // 当前页数
                currentPage : 1,
                // 每页条目数
                pageSize : 10,
                // 分页切换
                pageChange: function(pageNum){
                    let self = this;
                    self.table.currentPage = pageNum;
                    self.loadTable().then(function(res){
                        if(res.data){
                            self.table.data = res.data.data ? res.data.data : [];
                        }
                    });
                }
            }
        };
    },
    mounted(){
        let self = this;
        //加载客户列表
        self.loadTable().then(function(res){
            // console.log(res);
            if(res.data){
                self.table.data = res.data.data ? res.data.data : [];
            }
        });
    },
    methods: {
        loadTable(){
            let self = this;
            self.table.loading = true;

            return new Promise(function(resolve,reject){
                self.$http
                    .get('/app/v1/objectRunLogs',{
                        params: {
                            pageSize: 10,
                            currentPage: self.table.currentPage
                        }
                    })
                    .then(function(res) {
                        self.table.total = res.data.recordCount;
                        self.table.loading = false;
                        resolve(res);
                    },function(res){
                        self.table.loading = false;
                        reject(res);
                    });
            });
        }
    }
};
</script>
